create table conso (
numcons
integer
primary key ,
libcons
varchar(30)
not null,
prixcons
numeric(6,2) not
null check(prixcons between 1 and 10)
);
create
table
tables(
numtable
integer
primary key,
nomtable
varchar(30)
not null,
nbplaces
smallint not null
);
create
table
serveur (
numserv
integer
primary key,
nomserv
varchar(30)
not null,
rueserv
varchar(30),
cpserv
char(5),
villeserv
varchar(30),
datenaiss
date
);
create table
facture
(
numfact
integer
primary key,
numtable
integer
references tables(numtable) ,
numserv
integer
references serveur(numserv),
datefact
date not null
);
create
table
ligne_fact (
numfact
integer
references facture(numfact) on delete cascade,
numcons
integer
references conso(numcons),
qte
smallint not null,
primary key(numfact,numcons)
);
1) Ajouter un attribut commentaire, d'une
longueur de 80, dans la table des serveurs.
Alter table
serveur
2) Ajouter la contrainte "quantité facturée
obligatoirement positive" dans la table des lignes de facture.
Alter table
ligne_fact
Add constraint
ck_qte check(qte > 0);
3) Dans la table des serveurs, faire passer la
longueur de la ville à 35 caractères.
Alter table
serveur
alter column
villeserv varchar(35);
Alter table
serveur
add constraint
ck_un unique(nomserv);
select numfact,nomtable,datefact
from facture f
inner join tables t on
f.numtable=t.numtable;
select distinct libcons
from conso c
inner join ligne_fact f
on c.numcons = l.numcons
inner join facture f
on f.numfact = l.numfact
and datefact = '2006-02-22';
select count(*)
from serveur;
insert into
serveur
values (54,
'Lulu', '35, rue des Lilas', 69000, 'Lyon',
'1986-06-20');
select numtable, nbplaces
where numtable not in
(select numtable
from facture);
select libcons, prixcons
from conso
where prixcons =
(select min(prixcons)
from
conso);
select f.numfact, datefact, sum(prixcons * qte)
from conso c
inner join ligne_fact f
on c.numcons = l.numcons
inner join facture f
on f.numfact = l.numfact
group by f.numfact, datefact;
update conso
set prixcons=prixcons*1.1
where libcons
in ('Café', ‘Café double’, ‘Café crème’);
Ou bien (si on
considère qu’il n’y a pas d’autres consommations dont le nom
commence par café) :
update conso
set prixcons=prixcons*1.1
where libcons like 'Café%';
select nomserv
from conso c
inner join ligne_fact f
on c.numcons = l.numcons
inner join facture f
on f.numfact = l.numfact
inner join serveur s
on s.numserv = f.numserv
group by nomserv
having sum(prixcons * qte) > 15;
delete from ligne_fact
where
numfact=1207
and numcons=108;
Select nomserv,
date_part('year', CURRENT_DATE) - date_part('year',
datenaiss)
From serveur;